const vscode = require('vscode');
const util = require('./util');
const fs = require('fs');
module.exports = function(context) {
    // 注册HelloWord命令
    context.subscriptions.push(vscode.commands.registerCommand('extension.collect', () => {
    let collectObj = {
        filesList: [],
        dirsList: []
    };
    const projectPath = util.getProjectPath();
    collectObj = util.readFileList(projectPath+"\\src", collectObj)
    var nameMap = {}
    collectObj.filesList.forEach((item)=>{
        const content =  fs.readFileSync(item, 'utf-8');   //读取文件
        let srArray = content.split('\r\n');
        var start = false
        for (var i=0;i<srArray.length;i++){
            if(srArray[i].replace(/\s/g,"").startsWith("exportdefault")){
                start = true
            }
            if(start&&srArray[i].replace(/\s/g,"").startsWith("name:")){
                var arr = srArray[i].replace(/\s/g,"").replace("name:","").replace(/\W/g,"");
                nameMap[arr]=item
                return
            }
        }
    })
    
    fs.writeFile(projectPath+"\\sourcemap.json",JSON.stringify(nameMap),{flag:'w',encoding:'utf-8'},function(err){
        if(err){
            vscode.window.showInformationMessage('sourcemap文件写入失败！');
        }else{
            vscode.window.showInformationMessage('sourcemap文件写入成功！');
        }
   
   })
  
        
    }));
};